'use strict';

angular.module('app')
    .controller('updateController', ['$scope', '$rootScope', '$state', '$timeout',
        		function($scope, $rootScope, $state, $timeout) {
    	var title = "";
        //var defaultAva = $rootScope.defaultAvatar;
        //$scope.myImage = '';
        //$scope.myCroppedImage = '';
        if ($state.includes('**.activity.update')) {
            title = "修改活动";
            var id = $state.params.id;
            detail(id);
        } else if ($state.includes('**.activity.create')) {
            title = "添加活动";
        }
        
        $scope.title = $rootScope.title = title;
        $scope.loading = true;
        
        $scope.submit = function(){
            $scope.loading = true;

            if(!$('#theme').val()){
                $scope.alertModal({'content':'活动名称不能为空'});
                return;
            }
            
            if(!$('#city').val()){
                $scope.alertModal({'content':'主办地不能为空'});
                return;
            }
            
            if(!$('#hostAddr').val()){
                $scope.alertModal({'content':'活动主办地址不能为空'});
                return;
            }
            
            if(!$('#preStartTime').val()){
                $scope.alertModal({'content':'报名开始日期不能为空'});
                return;
            }
            
            if(!$('#preEndTime').val()){
                $scope.alertModal({'content':'报名结束日期不能为空'});
                return;
            }
            
            if (!checkEndTime($('#preStartTime').val(), $('#preEndTime').val())) {
            	$scope.alertModal({'content':'报名开始日期不能大于报名结束日期'});
        		return;
        	}
            
            if(!$('#activityStartTime').val()){
                $scope.alertModal({'content':'活动开始日期不能为空'});
                return;
            }
            
            if(!$('#activityEndTime').val()){
                $scope.alertModal({'content':'活动结束日期不能为空'});
                return;
            }
            
            if (!checkEndTime($('#activityStartTime').val(), $('#activityEndTime').val())) {
            	$scope.alertModal({'content':'活动开始时间不能大于活动结束时间'});
        		return;
        	}
            
            if ($('#activityStartTime').val() == $('#activityEndTime').val()) {
            	$scope.alertModal({'content':'活动开始时间不能等于活动结束时间'});
        		return;
        	}
            
            var preStartTime = $('#preStartTime').val() + " 00:00:00";
            var preEndTime = $('#preEndTime').val() + " 23:59:59";
            $scope.record.preStartTime = preStartTime;
            $scope.record.preEndTime = preEndTime;
            
            if (!checkEndTime(preStartTime, $('#activityStartTime').val())) {
            	$scope.alertModal({'content':'报名日期不能大于活动开始时间'});
        		return;
        	}
            
            if (!checkEndTime(preEndTime, $('#activityStartTime').val())) {
            	$scope.alertModal({'content':'报名日期不能大于活动开始时间'});
        		return;
        	}
            
            if (!checkEndTime(preEndTime, $('#activityEndTime').val())) {
            	$scope.alertModal({'content':'报名结束日期不能大于活动结束时间'});
        		return;
        	}
            
            /*if(!$('#detailUrl').val()){
                $scope.alertModal({'content':'活动详情不能为空'});
                return;
            }*/
            
            var detailUrlArr = $scope.detailImgData[0];
            if (!detailUrlArr) {
            	$scope.alertModal({'content':'请上传活动详情图片'});
                return;
            }

            var detailUrl = $scope.detailImgData[0].src;
            $scope.record.detailUrl = detailUrl[0];
            
            if(!$('#organizer').val()){
                $scope.alertModal({'content':'主办单位不能为空'});
                return;
            }
            
            if(!$('#contractor').val()){
                $scope.alertModal({'content':'承办单位不能为空'});
                return;
            }
            
            $scope.record.activityStartTime = $('#activityStartTime').val();
            $scope.record.activityEndTime = $('#activityEndTime').val();
            
            var picUrlArr = $scope.imgData[0];
            if (!picUrlArr) {
            	$scope.alertModal({'content':'请上传活动图片'});
                return;
            }

            var picUrl = $scope.imgData[0].src;
            $scope.record.picUrl = picUrl[0];
            
            var m = $scope.record;
            $scope.isDisabled = true;//提交disabled
            
            if (m) {
            	console.log($scope.record)
            	
                $.ajax({
    				url : $scope.record.id ? $scope.app.project_name + '/activity/update' : $scope.app.project_name + '/activity/add',
    				data: $scope.record
    			}).then(callback);
            }
            function callback(result) {
                if(result.httpCode ==200){//成功
                    $timeout(function(){
                    	$scope.isDisabled = false;
                        $state.go('main.funScenes.activity.list');
                    },2000);
                }else{
                	$scope.isDisabled = false;
                	$scope.alertModal({"content":"保存失败"});
                }
            }
        };
        
        function checkEndTime(startTime, endTime) {  
    	    var start = new Date(startTime.replace("-", "/").replace("-", "/"));  
    	    var end = new Date(endTime.replace("-", "/").replace("-", "/"));  
    	    if (end < start) {  
    	        return false;  
    	    }  
    	    return true;  
    	}
        
        // 初始化页面
        function detail(id) {
	        $scope.loading = true;
        	$.ajax({ 
				url : $scope.app.project_name + '/activity/read/detail',
				data: {'id': id}
			}).then(function(result) {
		        $scope.loading = false;
				if (result.httpCode == 200) {
					$scope.record = result.data;
		            $('#preStartTime').val($scope.record.preStartTime);
		            $('#preEndTime').val($scope.record.preEndTime);
		            $('#activityStartTime').val($scope.record.activityStartTime);
		            $('#activityEndTime').val($scope.record.activityEndTime);
		            $scope.imgData = [];
		            var srcArr = [];
		            srcArr.push($scope.record.picUrl);
		            $scope.imgData.push({
	                    src:srcArr
	                })
	                
	                $scope.detailImgData = [];
		            var detailArr = [];
		            detailArr.push($scope.record.detailUrl);
		            $scope.detailImgData.push({
	                    src:detailArr
	                })
				} else {
					$scope.msg = result.msg;
				}
				$scope.$apply();
			});
        }
}]);
